package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.vo.category.CategoryCreateVo;
import org.summer.lawyer.domain.vo.category.CategoryUpdateVo;
import org.summer.lawyer.domain.vo.category.CategoryVo;
import org.summer.lawyer.service.ICategoryService;

import java.util.List;

/**
 *
 */
@Tag(name = "后台相关接口", description = "后台服务类别管理")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/category")
public class CategoryController {

    private final ICategoryService categoryService;


    @Operation(description = "查询服务类别列表")
    @SaCheckPermission("spu:category:list")
    @GetMapping("/list")
    public TableDataInfo<CategoryVo> list(CategoryVo vo, PageQuery pageQuery) {
        return categoryService.selectPage(vo, pageQuery);
    }

    @Operation(description = "新增服务类别")
    @Log(title = "新增服务类别", businessType = BusinessType.INSERT)
    @SaCheckPermission("spu:category:add")
    @PostMapping("/saveCategory")
    public R<Void> saveCategory(@Validated @RequestBody CategoryCreateVo vo) {
        categoryService.saveCategory(vo);
        return R.ok();
    }

    @Operation(description = "修改服务类别")
    @Log(title = "修改服务类别", businessType = BusinessType.UPDATE)
    @SaCheckPermission("spu:category:edit")
    @PostMapping("/updateCategory")
    public R<Void> updateCategory(@Validated @RequestBody CategoryUpdateVo vo) {
        categoryService.updateCategory(vo);
        return R.ok();
    }

    @Operation(description = "删除服务类别")
    @Log(title = "删除服务类别", businessType = BusinessType.DELETE)
    @SaCheckPermission("spu:category:remove")
    @DeleteMapping("/{ids}")
    public R<Void> removeCategory(@NotEmpty(message = "主键不能为空") @PathVariable("ids")Long[] ids) {
        categoryService.deleteCategoryByIds(List.of(ids));
        return R.ok();
    }

    @Operation(description = "查询服务类别信息")
    @SaCheckPermission("spu:category:query")
    @GetMapping("/{id}")
    public R<CategoryVo> getInfo(@NotNull(message = "主键不能为空")@PathVariable("id") Long id){
        return R.ok(categoryService.selectCategoryVoById(id));
    }

    @SaCheckLogin
    @Operation(description = "获取服务分类")
    @GetMapping("/categoryTree")
    public R<List<Tree<Long>>> categoryTree(CategoryVo vo) {
        //CategoryTreeSelectVo selectVo = new CategoryTreeSelectVo();
       // selectVo.setCheckedKeys();
        //selectVo.setCategories(categoryService.selectCategoryTreeList(new CategoryVo()));
        return R.ok(categoryService.selectCategoryTreeList(vo));
    }
}
